package com.ideaplugin.datamanager;

import com.mybatisflex.codegen.Generator;
import com.mybatisflex.codegen.config.ColumnConfig;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.codegen.dialect.JdbcTypeMapping;
import com.zaxxer.hikari.HikariDataSource;

import java.math.BigInteger;
import java.time.LocalDateTime;
import java.util.Date;

public class Codegen {

    public static void main(String[] args) {
        //配置数据源
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl("jdbc:mysql://192.168.250.57:3306/dwh?serverTimezone=Asia/Shanghai&useSSL=false&unicode=true&characterEncoding=UTF-8");
        dataSource.setUsername("root");
        dataSource.setPassword("zjb");
        JdbcTypeMapping.registerMapping(LocalDateTime.class, Date.class);
        JdbcTypeMapping.registerMapping(BigInteger.class, Long.class);

        //创建配置内容，两种风格都可以。
        GlobalConfig globalConfig = createGlobalConfigUseStyle1();
        //GlobalConfig globalConfig = createGlobalConfigUseStyle2();

        //通过 datasource 和 globalConfig 创建代码生成器
        Generator generator = new Generator(dataSource, globalConfig);

        //生成代码
        generator.generate();
    }

    public static GlobalConfig createGlobalConfigUseStyle1() {
        //创建配置内容
        GlobalConfig globalConfig = new GlobalConfig();
        //设置根包
        globalConfig.getPackageConfig()
                .setSourceDir("D:\\idea_project\\base-manager\\dwh-data-manager\\src\\main\\java")
                .setBasePackage("com.ideaplugin.datamanager")
                .setServicePackage("com.ideaplugin.datamanager.manager")
                .setServiceImplPackage("com.ideaplugin.datamanager.manager.impl");

        final ColumnConfig columnConfig = new ColumnConfig();
        //columnConfig.setTypeHandler(new Codegen());
        //globalConfig.getStrategyConfig().setColumnConfig(new ColumnConfig());

        //设置表前缀和只生成哪些表
        //globalConfig.setTablePrefix("tb_");
        globalConfig.getStrategyConfig()
                .setGenerateTable("sys_menu");

        //设置生成 entity 并启用 Lombok
        globalConfig.enableEntity().setOverwriteEnable(true).setWithLombok(true);
        //设置生成 mapper
        globalConfig.enableMapper().setOverwriteEnable(true);
        globalConfig.enableService().setClassSuffix("Manager").setOverwriteEnable(true);
        globalConfig.enableServiceImpl().setClassSuffix("ManagerImpl").setOverwriteEnable(true);


        //可以单独配置某个列
        //ColumnConfig columnConfig = new ColumnConfig();
        //columnConfig.setColumnName("tenant_id");
        //columnConfig.setLarge(true);
        //columnConfig.setVersion(true);
        //globalConfig.getStrategyConfig()
        //        .setColumnConfig("tb_account", columnConfig);


        return globalConfig;
    }

}